<?php
// Max height and width and base path EDIT HERE 
$max_width = 128;
$max_height = 128;

// DONT EDIT BELOW

$directory = $_GET['file'];
str_replace('\\', '/', $directory);
$lastChar = end($directory);

$upfile = $directory;

$ext = strtolower(end(explode('.', $directory)));
if($ext == 'jpg' || $ext == 'jpeg'){
	
	Header("Content-type: image/jpeg");
	
	$size = getimagesize($upfile); // Read the size
	$width = $size[0];
	$height = $size[1];
	// Proportionally resize the image to the
	// max sizes specified above
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	if( ($width <= $max_width) && ($height <= $max_height) ){
	   $tn_width = $width;
	   $tn_height = $height;
	}elseif (($x_ratio * $height) < $max_height){
	   $tn_height = ceil($x_ratio * $height);
	   $tn_width = $max_width;
	}else{
	   $tn_width = ceil($y_ratio * $width);
	   $tn_height = $max_height;
	}
	
	// Increase memory limit to support larger files    
	ini_set('memory_limit', '32M');
	
	// Create the new image!
	$src = imagecreatefromjpeg($upfile);
	$dst = imagecreatetruecolor($tn_width, $tn_height);
	imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	imagejpeg($dst);
	
}elseif($ext == "png"){
	
	Header("Content-type: image/png");
	
	$size = getimagesize($upfile); // Read the size
	$width = $size[0];
	$height = $size[1];
	// Proportionally resize the image to the
	// max sizes specified above
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	if( ($width <= $max_width) && ($height <= $max_height) ){
	   $tn_width = $width;
	   $tn_height = $height;
	}elseif (($x_ratio * $height) < $max_height){
	   $tn_height = ceil($x_ratio * $height);
	   $tn_width = $max_width;
	}else{
	   $tn_width = ceil($y_ratio * $width);
	   $tn_height = $max_height;
	}
	
	// Increase memory limit to support larger files    
	ini_set('memory_limit', '32M');
	
	// Create the new image!
	$dst = imagecreatetruecolor($tn_width, $tn_height);
	imagealphablending($dst, false);
	imagesavealpha($dst, true); 
	
	$src = imagecreatefrompng($upfile);
	imagealphablending($src, true);
	
	imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	
	imagepng($dst);
	
}elseif($ext == "gif"){
	
	Header("Content-type: image/gif");
	
	$size = getimagesize($upfile); // Read the size
	$width = $size[0];
	$height = $size[1];
	// Proportionally resize the image to the
	// max sizes specified above
	$x_ratio = $max_width / $width;
	$y_ratio = $max_height / $height;
	if( ($width <= $max_width) && ($height <= $max_height) ){
	   $tn_width = $width;
	   $tn_height = $height;
	}elseif (($x_ratio * $height) < $max_height){
	   $tn_height = ceil($x_ratio * $height);
	   $tn_width = $max_width;
	}else{
	   $tn_width = ceil($y_ratio * $width);
	   $tn_height = $max_height;
	}
	
	// Increase memory limit to support larger files    
	ini_set('memory_limit', '32M');
	
	// Create the new image!
	$src = imagecreatefromgif($upfile);
	$dst = imagecreatetruecolor($tn_width, $tn_height);
	imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
	imagegif($dst);
}


// Destroy the images
ImageDestroy($src);
ImageDestroy($dst);
?> 